//
//  YWPatrolLocationTableViewCell.m
//  maintain
//
//  Created by Kyo on 16/10/11.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWPatrolLocationTableViewCell.h"
#import "Masonry.h"
#import "YWMacro.h"
#import "YWCommonTool.h"

#import "YWLocation.h"
#import "YWBuildingManager.h"

@interface YWPatrolLocationTableViewCell()<UITextFieldDelegate>
@property (nonatomic ,strong) UILabel *locationLabel;
@property (nonatomic ,strong) UITextField *noteTextField;
@end
@implementation YWPatrolLocationTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }
    
    return self;
}

- (void)setUpSubviews{
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.height.equalTo(@(18));
    }];
    [self.noteTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.locationLabel.mas_bottom).offset(5);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
}

- (void)setLocation:(YWLocation *)location{
    _location = location;
    NSString *locationText = [[YWBuildingManager shareManager] areaLocationNobuildingWithId:location.locationId];
    self.locationLabel.text =  locationText;
    
    if (location.note.length) {
        self.noteTextField.text = location.note;
    }else{
        self.noteTextField.text = nil;
    }
}

- (void)setTextFieldPlaceholder:(NSString *)textFieldPlaceholder{
    _textFieldPlaceholder = textFieldPlaceholder;
    self.noteTextField.placeholder = textFieldPlaceholder;
}

- (UILabel *)locationLabel{
    if (!_locationLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _locationLabel = label;
    }
    return _locationLabel;
}

- (UITextField *)noteTextField{
    if (!_noteTextField) {
        UITextField * textField = [[UITextField alloc] init];
        textField.placeholder = NSLocalizedString(@"writeLocationNote", nil);
        textField.font = kContentFont;
        textField.delegate = self;
        [textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
        [self.contentView addSubview:textField];
        _noteTextField = textField;
    }
    return _noteTextField;
}

- (void)textFieldChange:(UITextField*)textfield{
    NSString *toBeString = textfield.text;
    //获取高亮部分
    UITextRange *selectedRange = [textfield markedTextRange];
    UITextPosition *position = [textfield positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > 100)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:100];
            if (rangeIndex.length == 1)
            {
                textfield.text = [toBeString substringToIndex:100];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 100)];
                textfield.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    _location.note = textfield.text;
}

@end
